<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of NetworkController
 *
 * @author mateusz.ledzewicz
 */
class DataController extends FormListController {
    protected $type;
    protected $sourceType;
    protected function begin() {
       
        $type = $_GET['data'];
        $this->sourceType = iSset($_GET['type']) ? $_GET['type'] : '';
        if($type == 'train')
        {
             $this->view = new ViewTrain();
        } 
        else if($type == 'test')
        {
             $this->view = new ViewTrain();
        } else{
            throw new Exception ("Bad type of data view");
        }
        $this->type=$type;
        if(!in_array($this->sourceType, array('gpw', 'yahoo',''))){
            throw new Exception("Bad sourceType");
        }
        $this->view->setVar('type',$this->sourceType);
        $this->view->setVar('data',$this->type);
            
    }

    protected function listRun(){
        $getAll = ($this->type == 'train') ? Training_data::getAll() : Test_data::getAll();
        $model = ($this->type == 'train') ? Training_data::loadModel() : Test_data::loadModel();
        $this->view->setSpecialVar('list', $getAll);
        $this->view->setModel($model, 'header'); 
    }
    
    protected function formRun(){
        $id = (isSet($_GET['id']) && !empty($_GET['id'])) ? $_GET['id'] : null;
        $model = ($this->type == 'train') ? Training_data::loadModel($id) : Test_data::loadModel($id) ;
        $where = ($this->sourceType == 'gpw') ? "where instrumentType = 'gpw'" : "where instrumentType = 'yahoo'";
        $select = Instrument::getAll($where);
        
        $this->view->setSpecialVar('select', $select);
        
        
       
        if(count($_POST) > 0){
            //print_r($_POST); exit;
            $paramsIn = array();  
            $model->setValues($_POST);
            
            if($model->NewRecord && Training_data::loadSpecialModel("name = '{$model->name}' ")->NewRecord == FALSE)
            {
                throw new Exception('Data with this name exists');
            }
            
            $instrument = Instrument::loadModel($_POST['isin']);
            if(is_null($instrument) || $instrument->NewRecord)
            {
                throw new Exception('Instrument with number '.$_POST['isin'].' doesn\'t exist');
            }
            $model->file = ($this->type == 'train') ? ConfigPath::trainingData_dir : ConfigPath::testData_dir;
            $model->file .= BasicFunctions::prepareFileName($model->name).ConfigSuffix::trainingData_ext;
            
            $model->id_instrument = $instrument;
            $model->data_in = DataGenerator::inNormal;
            
            $config['dateTo'] = $model->dateTo;
            $config['isin'] = $instrument ->isin;
            $config['type'] = $_GET['type'];
            $model -> data_out = $_POST['outType'][0];
            $dataOut = constant("DataGenerator::out{$model->data_out}");
            if(isSet($_POST['money']) && $_POST['money'] != 'none' )
            {
                $paramsIn[] = new Param(Param::typeValue, Param::currency, $_POST['money']);
                $model->data_in .= '_'.$_POST['money'];
            }
            foreach($_POST['paramType'] as $key => $type)
            {
                $param = new Param($type, $_POST['paramParam'][$key], $_POST['paramValue'][$key]);
                $paramsIn[] = $param;
                $model->data_in.=$param->toString();
                
            }
           
            
            $paramOut = new ParamOut($_POST['outDays'][0], $_POST['outPercent'][0], $_POST['outAvgdays'], $_POST['outValue'][0]);
            $model->data_out = $paramOut->toString($model->data_out);
            
            
            $test = new DataGenerator(DataGenerator::inNormal, $dataOut, $config);
            $test ->generateData($model->number, $paramsIn, $paramOut , $_POST['days']);
            
            
            
            
            $test->generateFile($model->file); 
            
            $model->number_in = $test->getNumberIn();
            $model->number_out = $test->getNumberOut();
            
            
            $model->save();
            header("Location: index.php?step=data&data=".$this->type);
       
       }
       $this->view->setModel($model, 'edit');
    }


        
        
        //put your code here
}

?>
